//
//  listNode.swift
//  206-翻转链表
//
//  Created by lhc on 2023/9/20.
//

import Foundation

class ListNode<T> {
    let val: T
    var next: ListNode<T>?
    
    init(val: T, next: ListNode<T>? = nil) {
        self.val = val
        self.next = next
    }
    
    static func initWithList(list: [T]) -> ListNode<T>? {
        let size = list.count
        guard size != 0 else {return nil}
        
        let head = ListNode(val: list.first!)
        var cur = head
        
        for i in 1..<size {
            let n = ListNode(val: list[i])
            cur.next = n
            cur = n
        }
        
        return head
    }
}

extension ListNode: CustomStringConvertible {
    var description: String {
        var des = "\(self.val)"
        var cur = self.next
        while cur != nil {
            des += " -> \(cur!.val)"
            cur = cur!.next
        }
        return des
    }
    
    
}
